package com.lagou.service.impl;

import com.alibaba.fastjson.serializer.StringCodec;
import com.lagou.base.StatusCode;
import com.lagou.dao.CourseContentDao;
import com.lagou.dao.CourseDao;
import com.lagou.dao.impl.CourseContentDaoImpl;
import com.lagou.pojo.Course;
import com.lagou.pojo.Course_Lesson;
import com.lagou.pojo.Course_Section;
import com.lagou.service.CourseContentService;
import com.lagou.utils.DateUtils;
import com.lagou.utils.DruidUtils;

import javax.sql.DataSource;
import java.util.List;

public class CourseContentServiceImpl implements CourseContentService {
    /**
     * 课程内容管理 Service层实现类
     * */
    CourseContentDao contentDao = new CourseContentDaoImpl();

    @Override
    public List<Course_Section> findSectionAndLessonByCourseId(int courseId) {
        //获取数据查询层的对象
        CourseContentDao contentDao =new CourseContentDaoImpl();
        List<Course_Section> sections = contentDao.findSectionAndLessonByCourseId(courseId);
        return sections;
    }

    /**
     * 根据课程id  获取回显信息（获取课程名称）
     * @param courseId
     * @return
     */
    @Override
    public Course findCourseByCourseId(int courseId) {
        Course course = contentDao.findCourseByCourseId(courseId);
        return course;
    }

    /**
     * 保存课程信息 （用户新建 章节信息）
     * @param section
     * @return
     */
    @Override
    public String saveSection(Course_Section section) {
        section.setStatus(2); //状态，0:隐藏；1：待更新；2：已发布
        String date = DateUtils.getDateFormart();
        section.setCreate_time(date);
        section.setUpdate_time(date);

        //2.调用Dao进行插入
        int i = contentDao.saveSection(section);
        //3.根据插入是否成功,封装对应信息
        if(i>0){
            //保存成功
            String result = StatusCode.SUCCESS.toString();
            return result;
        }else {
            //保存失败
            String result = StatusCode.FAIL.toString();
            return result;
        }
    }

    /**
     * 修改章节信息
     * @param section
     * @return
     */
    @Override
    public String updataSection(Course_Section section) {

        //1、补全信息
        String date = DateUtils.getDateFormart();
        section.setUpdate_time(date);
        //2、调用dao层进行数据查询
        int row = contentDao.updateSection(section);

        //3、判断查询结果  数据封装
        if(row >0){
            //插入成功
            String result = StatusCode.SUCCESS.toString();
            return result;
        }else{
            //插入失败
            String result = StatusCode.FAIL.toString();
            return result;
        }
    }

    /**
     * 修改状态信息
     * @param id
     * @param Status
     * @return
     */
    @Override
    public String updateSectionStatus(int id, int Status) {
        int i = contentDao.updateSectionStatus(id, Status);
        //判断返回结果
        if(i > 0){
            String result = StatusCode.SUCCESS.toString();
            return  result;
        } else {
            String result = StatusCode.FAIL.toString();
            return  result;
        }

    }

    /**
     * 作业 -----------------------------------------------------------------
     *  添加课时时 ，回显章节信息
     * @param sectionId
     * @return
     */
    @Override
    public Course_Section findSectionBySectionId(int sectionId) {
        Course_Section section = contentDao.findSectionBySectionId(sectionId);
        return  section;
    }

    /**
     *  新建课时信息功能
     * @param lesson
     * @return
     */
    @Override
    public String saveLesson(Course_Lesson lesson) {
        //1.补全章节信息

        String date = DateUtils.getDateFormart();
        lesson.setUpdate_time(date);
        lesson.setCreate_time(date);

        //2.调用Dao进行插入
        int row = contentDao.saveLesson(lesson);
        //3.根据插入是否成功,封装对应信息
        if(row > 0){
            //保存成功
            String result = StatusCode.SUCCESS.toString();
            return result;
        }else{
            String result =StatusCode.FAIL.toString();
            return result;
        }

    }

    /**
     * 修改课时内容
     * @param lesson
     * @return
     */
    @Override
    public String updateLesson(Course_Lesson lesson) {

        //1.补全章节信息

        String date = DateUtils.getDateFormart();
        lesson.setUpdate_time(date);


        //2.调用Dao进行插入

        int i = contentDao.updateLesson(lesson);

        //3.根据修改是否成功,封装对应信息

        if(i > 0) {
            //保存成功
            String result = StatusCode.SUCCESS.toString();
            return result;
        }else {
            String result =StatusCode.FAIL.toString();
            return result;
        }
    }


}
